<template>
  <el-row>
    <el-col :span="24">
      <dynForm
        ref="dataForm"
        :info-form="searchForm"
        :search-tags="searchTags"
        :select-list="selectList"/>
    </el-col>
  </el-row>
</template>

<script>
// import { getCheckFormNo } from '@/api/checkManagement/checkListGenerate'
import { getEqpIdListByType } from '@/api/equipment/eqpbase'
import { selectitemlist } from '@/api/system/attributes'
import { getLotNo } from '@/api/checkManagement/sampleLimit'

function throttle(fn, delay = 1000) {
  let t = null
  let begin = new Date().getTime()
  return function() {
    const current = new Date().getTime()
    clearTimeout(t)
    if (current - begin >= delay) {
      fn()
      begin = current
    } else {
      t = setTimeout(() => {
        fn()
      }, delay)
    }
  }
}
export default {
  name: 'ProductionChecklistGenerate',
  data() {
    const searchTags = [
      { label: this.$t('checkFormNumber'), prop: 'checkFormId', tag: 'DynSelect', event: (value) => {
        const findItem = this.selectList['checkFormId'].find(item => item.id === value)
        this.checkFormNotype = findItem ? findItem.type : null
        Object.keys(this.searchForm).forEach(e => {
          if (e !== 'checkFormId') {
            this.searchForm[e] = undefined
          }
        })
      } },
      { label: this.$t('checkOpportunity'), prop: 'checkOpportunityId', tag: 'DynInput', vif: () => {
        return [2, 3, 4, 5].includes(this.checkFormNotype)
      } },
      { label: '分选机', prop: 'eqpId', tag: 'DynSelect', vif: () => {
        return [3, 4].includes(this.checkFormNotype)
      } },
      { label: '测试机', prop: 'testEqpId', tag: 'DynInput', vif: () => {
        return [3, 4].includes(this.checkFormNotype)
      } },
      { label: '随工单编号', prop: 'workOrderNo', tag: 'DynInput', vif: () => {
        return [3, 4, 5].includes(this.checkFormNotype)
      }, event: throttle((value) => {
        getLotNo({ LotNo: value }).then(res => {
          this.selectList['t'] = res.data
        })
      }, 1000) },
      { label: '样管编号', prop: 'sampleIdList', tag: 'DynSelect', multiple: true, vif: () => {
        return this.checkFormNotype === 3
      } },
      { label: '测试程序', prop: 'testProgram', tag: 'DynInput', vif: () => {
        return [3, 4].includes(this.checkFormNotype)
      } },
      { label: '测试站编号', prop: 'enumTestStateValues', tag: 'DynSelect', multiple: true, vif: () => {
        return this.checkFormNotype === 3
      } },
      { label: '设备名称', prop: 'equipmentName', tag: 'DynInput', vif: () => {
        return this.checkFormNotype === 2
      } }
    ]
    return {
      checkFormNotype: null,
      searchForm: {},
      selectList: {},
      searchTags: Object.freeze(searchTags)
    }
  },
  created() {
    this.getFormCheckList()
    this.getHandlerTypes()
    // this.getTesterTypes()
    this.getTesterNumber()
  },
  methods: {
    submitForm() {
      return new Promise((resolve, reject) => {
        this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
          resolve(valid)
        })
      })
    },
    getYangguan: function(value) {
      throttle(() => {
        getLotNo({ LotNo: value }).then(res => {
          console.log(res)
        })
      }, 500)
    },
    async getFormCheckList() {
      // const res = await getCheckFormNo()
      const arr = [
        {
          'id': 'A727CCB7AC185E1A',
          'name': 'CMSForm2',
          'type': 2,
          'checkOpportunityId': '6BD6BB18E180425ABEEB96376D418B60',
          'checkOpportunityName': '113'
        },
        {
          'id': 'A727CC2DFE2BE411AB7AC185E1A',
          'name': 'CMSForm3',
          'type': 3,
          'checkOpportunityId': '6BD6BB18E180425ABEEB96376D418B60',
          'checkOpportunityName': '113'
        },
        {
          'id': 'A727CC2DFE244CE7AC185E1A',
          'name': 'CMSForm4',
          'type': 4,
          'checkOpportunityId': '6BD6BB18E180425ABEEB96376D418B60',
          'checkOpportunityName': '113'
        },
        {
          'id': 'DFE244CEEBE411AB7AC185E1A',
          'name': 'CMSForm5',
          'type': 5,
          'checkOpportunityId': '6BD6BB18E180425ABEEB96376D418B60',
          'checkOpportunityName': '113'
        }
      ]
      this.$set(this.selectList, 'checkFormId', arr)
    },
    async getHandlerTypes() {
      const res = await getEqpIdListByType({ EqpType: 'HANDLER' })
      this.$set(this.selectList, 'eqpId', res.data)
    },
    // async getTesterTypes() {
    //   const res = await getEqpIdListByType({ EqpType: 'TESTER' })
    //   this.$set(this.selectList, 'd', res.data)
    // },
    async getTesterNumber() {
      const res = await selectitemlist({ name: 'TEST_STATION' })
      this.$set(this.selectList, 'enumTestStateValues', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
 ::v-deep .el-form .el-form-item {
    width: 45%;
    .el-form-item__content{
        width: calc(100% - 120px);
        .el-select{
            width: 100%;
        }
    }
}
</style>
